/**
 * 
 */
package com.pos.net.request;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.pos.R;
import com.pos.net.base.AuthFailureError;
import com.pos.net.base.RequestQueue;
import com.pos.net.base.Response;
import com.pos.net.base.VolleyError;
import com.pos.net.request.interfa.LoadListener;
import com.pos.net.request.load.SDownloadRequest;
import com.pos.net.toolbox.Volley;
import com.pos.set.AppSet;

import java.util.Map;

/**
 * @description:下载请求
 * @author cai
 * @time:2015年10月15日上午11:00:22
 */
public class DownloadRequest {
	private static DownloadRequest downloadRequest;
	private Context mContext;
	/**
	 * 请求队列
	 */
	private static RequestQueue mRequestQueue;
	/**
	 * 请求队列（不带缓存）
	 */
	private static RequestQueue mRequestQueueNoCache;

	private DownloadRequest(Context context) {
		mContext = context;
	}

	public synchronized static DownloadRequest getInstance(Context context) {
		if (downloadRequest == null) {
			downloadRequest = new DownloadRequest(context);
		}
		return downloadRequest;
	}

	/**
	 * 开始下载
	 * 
	 * @param url
	 *            下载地址
	 * @param requestId
	 *            请求id
	 * @param fileDir
	 *            文件路径
	 * @param isResume
	 *            是否支持断点
	 * @param listener
	 *            下载监听事件
	 * @return SDownloadRequest SDownloadRequest实例，调用stopDownload()可停止下载
	 */
	public SDownloadRequest startDownload(final String url, final int requestId, String fileDir, final boolean isResume,
                                          final LoadListener listener) {

		listener.onStart(requestId);
		//网络不通，并且不是内网测试状态(即外网环境)
		if (!isNetworkOk(mContext)&&!AppSet.testModel) {
			listener.onError(requestId, 0, mContext.getString(R.string.net_request_wrong));
			return null;
		} else {

			SDownloadRequest request = new SDownloadRequest(url, new Response.Listener<String>() {

				@Override
				public void onResponse(String response) {
					listener.onSuccess(requestId, url, response);
				}
			}, new Response.ErrorListener() {

				@Override
				public void onErrorResponse(VolleyError error) {
					listener.onError(requestId, 0, mContext.getString(R.string.net_request_fail));

				}

			}, new Response.LoadingListener() {

				@Override
				public void onLoading(long count, long current) {
					listener.onLoading(count, current);

				}
			}) {

				@Override
				public Map<String, String> getHeaders() throws AuthFailureError {
					Map<String, String> headers = super.getHeaders();
					// if (headers != null) {
					// headers.putAll(requestInfo.getHeaders());
					// } else {
					// headers = requestInfo.getHeaders();
					// }
					return headers;
				}
			};
			request.setResume(isResume);
			request.setTarget(fileDir);
			getRequestQueueNoCache().add(request);
			return request;
		}
	}
		
	/**
	 * 获取网络请求队列
	 */
	public RequestQueue getRequestQueue() {
		if (mRequestQueue == null) {
			mRequestQueue = Volley.newRequestQueue(mContext);
		}
		return mRequestQueue;
	}
	
	/**
	 * 获取不带缓存的网络请求队列
	 */
	public RequestQueue getRequestQueueNoCache() {
		if (mRequestQueueNoCache == null) {
			mRequestQueueNoCache = Volley.newNoCacheRequestQueue(mContext);
		}
		return mRequestQueueNoCache;
	}

	
	
	
	

	/**
	 * 检验网络是否有连接，有则true，无则false
	 * 
	 * @param context
	 */
	public boolean isNetworkOk(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni != null && ni.isConnected()) {
			return true;
		}
		return false;
	}

}
